package com.gxs.tga.poc.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gxs.tga.poc.domain.TgaSolution;
import com.gxs.tga.poc.domain.TgaWorksheet;
import com.gxs.tga.poc.domain.exceptions.DuplicateTgaWorksheet;
import com.gxs.tga.poc.domain.exceptions.TgaWorksheetNotFound;
import com.gxs.tga.poc.repository.TgaWorksheetRepository;

@Service
public class TgaWorksheetServiceImpl implements TgaWorksheetService
{

	@Resource
	private TgaWorksheetRepository tgaWorksheetRepository;

	@Override
	@Transactional
	public TgaWorksheet create(TgaWorksheet worksheet) throws DuplicateTgaWorksheet
	{
		TgaWorksheet existingTR = this.findById(worksheet.getSheetInstanceId() );
		if(existingTR != null)
		{
			throw new DuplicateTgaWorksheet();
		}
		TgaWorksheet createdWorksheet = worksheet;
		
		return tgaWorksheetRepository.save(createdWorksheet);
	}

	@Override
	@Transactional
	public TgaWorksheet findById(String sheetInstanceId)
	{
		return tgaWorksheetRepository.findOne(sheetInstanceId);
	}

	@Override
	@Transactional(rollbackFor = TgaWorksheetNotFound.class)
	public TgaWorksheet delete(String sheetInstanceId) throws TgaWorksheetNotFound
	{
		TgaWorksheet deletedWorksheet = tgaWorksheetRepository.findOne(sheetInstanceId);

		if (deletedWorksheet == null)
			throw new TgaWorksheetNotFound();

		tgaWorksheetRepository.delete(deletedWorksheet);
		return deletedWorksheet;
	}

	@Override
	@Transactional
	public List<TgaWorksheet> findAll()
	{
		return tgaWorksheetRepository.findAll();
	}

	@Override
	@Transactional(rollbackFor=TgaWorksheetNotFound.class)
	public TgaWorksheet update(TgaWorksheet worksheet) throws TgaWorksheetNotFound 
	{
		TgaWorksheet updatedWorksheet = tgaWorksheetRepository.findOne(worksheet.getSheetInstanceId());
		
		if (updatedWorksheet == null)
			throw new TgaWorksheetNotFound();
		
		updatedWorksheet.setParentWorksheetId(worksheet.getParentWorksheetId());
		updatedWorksheet.setSheetDataBlob(worksheet.getSheetDataBlob());
		updatedWorksheet.setSheetId(worksheet.getSheetId()); 
		//updatedWorksheet.setSheetInstanceId(sheetInstanceId); // Primary Key - can't update this
		updatedWorksheet.setSheetName(worksheet.getSheetName());
		updatedWorksheet.setShellId(worksheet.getShellId());
		updatedWorksheet.setStatusCollected(worksheet.getStatusCollected());
		updatedWorksheet.setStatusPreprod(worksheet.getStatusPreprod());
		updatedWorksheet.setStatusProd(worksheet.getStatusProd());
		updatedWorksheet.setStatusPsiqa(worksheet.getStatusPsiqa());
		updatedWorksheet.setTgaSolution(worksheet.getTgaSolution());
		
		this.tgaWorksheetRepository.saveAndFlush(updatedWorksheet);
		
		return updatedWorksheet;
	}

	@Override
	@Transactional
	public List<TgaWorksheet> findBySheetId(String sheetId)
	{
		return this.tgaWorksheetRepository.findBySheetId(sheetId);
	}

	@Override
	@Transactional
	public List<TgaWorksheet> findByShellId(String shellId)
	{
		return this.tgaWorksheetRepository.findByShellId(shellId);
	}

	@Override
	@Transactional
	public List<TgaWorksheet> findByTgaSolution(TgaSolution tgaSolution)
	{
		return this.tgaWorksheetRepository.findByTgaSolution(tgaSolution);
	}

	@Override
	@Transactional
	public List<TgaWorksheet> findByTgaSolutionAndShellId(TgaSolution tgaSolution, String shellId)
	{
		return this.tgaWorksheetRepository.findByTgaSolutionAndShellId(tgaSolution,  shellId);
	}


}
